package cc.ufinity.networkdiagnose.widget;

import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;

import org.achartengine.ChartFactory;
import org.achartengine.GraphicalView; 
import org.achartengine.model.CategorySeries;
import org.achartengine.renderer.DialRenderer;
import org.achartengine.renderer.DialRenderer.Type;
import org.achartengine.renderer.SimpleSeriesRenderer;

import cc.ufinity.networkdiagnose.view.R;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;


public class DialChartActivity extends Activity {
public CategorySeries category;
public DialRenderer renderer;
private GraphicalView mChartView;

private long xhqd=0;

Thread mediathread;
    final Handler handler = new Handler() {
        public void handleMessage(Message msg) {
            switch (msg.what) {
            case 1:
                mChartView.invalidate();
                break;
            }
            super.handleMessage(msg);
        }
    };
    Timer timer = new Timer();
    TimerTask task = new TimerTask() {
        public void run() {
            Message message = new Message();
            message.what = 1;
            handler.sendMessage(message);
        }
    };

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        category = new CategorySeries("");
        renderer = new DialRenderer();
        setContentView(R.layout.main12);
        category.add("Current", 0);//���Ǳ�һ����ʼֵ����Ȼ����
        
        renderer.setChartTitleTextSize(20);
        renderer.setLabelsTextSize(15);
        renderer.setLegendTextSize(15);//˵������Ĵ�С
        SimpleSeriesRenderer r = new SimpleSeriesRenderer();
        r.setColor(Color.RED);//ָ�����ɫ
        
        renderer.addSeriesRenderer(r);
        renderer.setLabelsTextSize(8);//�Ǳ��������С
        renderer.setLabelsColor(Color.GREEN);//�Ǳ��̸��ӵ���ɫ
        renderer.setShowLabels(true);
        //����ָ����ʽ��Ĭ��Ϊ����Σ���Ϊ��ͷ
        renderer.setVisualTypes(new DialRenderer.Type[] {Type.ARROW});
        renderer.setMinValue(-100);//�Ǳ�����Сֵ
        renderer.setMaxValue(100);//�Ǳ������ֵ
        timer.schedule(task, 0, 1000 * 1); //����timer,1�����һ��
        new ValuesThread().start();
    }
    
    /**
    * �� onResume ����л���ʱ�Զ�����
    */
    @SuppressWarnings("deprecation")
	@Override
    protected void onResume() {
     super.onResume();
     if (mChartView == null) {
      LinearLayout layout = (LinearLayout) findViewById(R.id.chart);
      mChartView = ChartFactory.getDialChartView(DialChartActivity.this, category, renderer);
      layout.addView(mChartView, new LayoutParams(
      LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
     } else {
      mChartView.setBackgroundResource(R.id.chart);
      mChartView.repaint();
     }
    }    
    private class ValuesThread extends Thread{
     Random random = new Random(100);//ָ��������������
  @Override
        public void run() {
         while(true){
                try {
                 category.clear();
                 xhqd=random.nextInt()%100;//�õ������-100��100
                 category.add("�Ǳ���", xhqd);
                    Thread.sleep(1000);
                }catch(InterruptedException e) {
                 e.printStackTrace();
                }
            }
        }
    }
}